/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.myapp.struts;

import com.myapp.beans.CreateSurveyFormBean;
import com.myapp.beans.Helper;
import com.myapp.biz.QuestionBiz;
import com.myapp.biz.SurveyBiz;
import com.myapp.entities.Question;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.w3c.dom.Document;

/**
 *
 * @author Vinh_Dragon
 */
public class CreateSurveyAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String Delete = "delete";
    private static final String Update = "update";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        CreateSurveyFormBean formbean = (CreateSurveyFormBean) form;
        QuestionBiz biz = new QuestionBiz();
        String questionId = formbean.getQuestionList();
        String mission = formbean.getMission();
        JOptionPane.showMessageDialog(null, "mission: "+mission);
        //String index = PropertyUtils.getIndexedProperty(formbean, "mission").toString();

        if (mission.equals("Create survey")) {
            if (questionId == "Select a question") {
                JOptionPane.showMessageDialog(null, "Select a question");
                return mapping.findForward(SUCCESS);
            }

            ArrayList<Question> list = new ArrayList<Question>();
            
            list = biz.getListQuestion(this.getServlet().getServletContext().getRealPath("/"));

            Question ques = null;
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getId().equals(questionId)) {
                    ques = list.get(i);
                }
            }

            ArrayList<String> listAnsw = new ArrayList<String>();
            listAnsw = ques.getAnsList();

            SurveyBiz surbiz = new SurveyBiz();

            Helper help = new Helper();
            String path = this.getServlet().getServletContext().getRealPath("/") + "surveys.xml";
            Document doc = help.getDocument(path);
            int id = 0;

            for (int i = 0; i < doc.getDocumentElement().getChildNodes().getLength(); i++) {
                if (doc.getDocumentElement().getChildNodes().item(i).getNodeName().equalsIgnoreCase("survey")) {
                    id++;
                }
            }

            surbiz.createElement(doc, String.valueOf(id + 1), ques, listAnsw, path);
            return mapping.findForward(SUCCESS);
        }else if(mission.equals("Delete question")){
            Helper help = new Helper();
            String path = this.getServlet().getServletContext().getRealPath("/") + "questions.xml";
            Document doc = help.getDocument(path);
            biz.deleteQuestion(doc, questionId, path);
            
            return mapping.findForward(Delete);
        }else if(mission.equals("Update question")){

            return mapping.findForward(Update);
        }else{
            return mapping.findForward("error");
        }
        //JOptionPane.showMessageDialog(null, "ID: " + id + "- path:" + path + " - length: " + listAnsw.size() + " - doc: " + doc.getChildNodes().getLength());
        
    }
}
